/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package trustevaluation;



/**
 *
 * @author César Andrés
 */

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class SQL {
    	private Connection connect = null;
	private Statement statement = null;
	

        static Map sql = new HashMap();
        private SQL()
        {
            connect();
        }
        
        public static SQL getSQL(Object t)
        {
            SQL s;
            if (!sql.containsKey(t))
            {
                s = new SQL();
                sql.put(t, s);
             }
            else
            {
                s = (SQL)sql.get(t);
                if(s.statement==null || s.connect ==null)
                {
                    s.connect();
                }
            }
            return s; 
        }
        /**
         * It is made a conection with the SQL
         */
        public final boolean connect()
        {
        		try {
			// This will load the MySQL driver, each DB has its own driver
			Class.forName("com.mysql.jdbc.Driver");

			// Setup the connection with the DB
			connect = DriverManager
					.getConnection("jdbc:mysql://"+Globals.SERVER+"/"+Globals.DATABASE+"?"
							+ "user="+Globals.USER+"&password="+Globals.PASSWORD);

			// Statements allow to issue SQL queries to the database
			statement = connect.createStatement();
                        
                        return true;
                        }
                        catch (Exception e)
                        {
                            System.out.println("Error SQL Connect");
                            return false;
                        }
        }
        
        	// You need to close the resultSet
	public void close() throws SQLException {
			

			if (statement != null) {
				statement.close();
			}

			if (connect != null) {
				connect.close();
			}
		
	}
        
        public ResultSet executeQuery(String query) throws Exception 
        {
        
                    ResultSet resultSet = null;
                    resultSet = statement.executeQuery(query);
                    
                    return resultSet;
        }
        public int executeUpdate(String query) throws Exception 
        {                    return statement.executeUpdate(query);}
        


}

